﻿using ExpenseSystem.Common;
using ExpenseSystem.Core.DataAddOn;
using ExpenseSystem.Fether;
using ExpenseSystem.Model;
using System;
using System.Collections.Generic;


namespace ExpenseSystem.Core.QuoteModules
{
    public class QuoteStateUpdateManager:QuoteManager
    {
        int rows;
        QuoteFetcher fetcher = new QuoteFetcher();
        QuoteDetailFetcher dFetcher = new QuoteDetailFetcher();
        RegionQuoteFetcher rFetcher = new RegionQuoteFetcher();
        TitleQuoteFetcher tFetcher = new TitleQuoteFetcher();
        SalesPointQuoteFetcher sFetcher = new SalesPointQuoteFetcher();
        QuoteManager orgQuoteManager;
        QuoteDetailManager odManager = new QuoteDetailBaseManager();
        RegionQuoteManager rManager = new RegionQuoteBaseManager();
        TitleQuoteManager tManager = new TitleQuoteBaseManager();
        SalesPointQuoteManager sManager = new SalesPointQuoteBaseManager();
        public QuoteStateUpdateManager(QuoteManager QuoteManager)
        {
            this.orgQuoteManager = QuoteManager;
        }

        public override bool Delete(QuoteModel Quote, out string message)
        {
            return orgQuoteManager.Delete(Quote, out message);
        }

        public override bool DeleteById(long[] QuoteId, out string message)
        {
            return orgQuoteManager.DeleteById(QuoteId, out message);
        }

        public override bool Save(QuoteModel Quote, out string message, params IAddable<QuoteModel>[] addables)
        {
            return orgQuoteManager.Save(Quote, out message, addables);
        }
        public override bool Approved(long id, out string message)
        {
            bool result = orgQuoteManager.Approved(id, out message);
            if (result)
            {
                IList<QuoteDetailModel> models = dFetcher.ListByMainId(profile.SiteId, id, out message, out rows);
                foreach (QuoteDetailModel model in models)
                {
                    model.ResetUpdateTrace();
                    model.State = Definition.STATE_APPROVED;
                    model.ApprovedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                    model.Approver = profile.UserCode;
                    if (!odManager.Save(model, out message)) { return false; }
                }
                IList<RegionQuoteModel> rmodels = rFetcher.ListByMainId(profile.SiteId, id, out message, out rows);
                foreach (RegionQuoteModel model in rmodels)
                {
                    model.ResetUpdateTrace();
                    model.State = Definition.STATE_APPROVED;
                    model.ApprovedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                    model.Approver = profile.UserCode;
                    if (!rManager.Save(model, out message)) { return false; }
                }
                IList<TitleQuoteModel> tmodels = tFetcher.ListByMainId(profile.SiteId, id, out message, out rows);
                foreach (TitleQuoteModel model in tmodels)
                {
                    model.ResetUpdateTrace();
                    model.State = Definition.STATE_APPROVED;
                    model.ApprovedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                    model.Approver = profile.UserCode;
                    if (!tManager.Save(model, out message)) { return false; }
                }
                IList<SalesPointQuoteModel> smodels = sFetcher.ListByMainId(profile.SiteId, id, out message, out rows);
                foreach (SalesPointQuoteModel model in smodels)
                {
                    model.ResetUpdateTrace();
                    model.State = Definition.STATE_APPROVED;
                    model.ApprovedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                    model.Approver = profile.UserCode;
                    if (!sManager.Save(model, out message)) { return false; }
                }
            }
            return result;
        }
        public override bool Comfirm(long id, out string message)
        {
            bool result = orgQuoteManager.Comfirm(id, out message);
            if (result)
            {
                IList<QuoteDetailModel> models = dFetcher.ListByMainId(profile.SiteId, id, out message, out rows);
                foreach (QuoteDetailModel model in models)
                {
                    model.ResetUpdateTrace();
                    model.State = Definition.STATE_CONFIRM;
                    model.ConfirmedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                    model.Confirmed = profile.UserCode;
                    if (!odManager.Save(model, out message)) { return false; }
                }
                IList<RegionQuoteModel> rmodels = rFetcher.ListByMainId(profile.SiteId, id, out message, out rows);
                foreach (RegionQuoteModel model in rmodels)
                {
                    model.ResetUpdateTrace();
                    model.State = Definition.STATE_CONFIRM;
                    model.ConfirmedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                    model.Confirmed = profile.UserCode;
                    if (!rManager.Save(model, out message)) { return false; }
                }
                IList<TitleQuoteModel> tmodels = tFetcher.ListByMainId(profile.SiteId, id, out message, out rows);
                foreach (TitleQuoteModel model in tmodels)
                {
                    model.ResetUpdateTrace();
                    model.State = Definition.STATE_CONFIRM;
                    model.ConfirmedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                    model.Confirmed = profile.UserCode;
                    if (!tManager.Save(model, out message)) { return false; }
                }
                IList<SalesPointQuoteModel> smodels = sFetcher.ListByMainId(profile.SiteId, id, out message, out rows);
                foreach (SalesPointQuoteModel model in smodels)
                {
                    model.ResetUpdateTrace();
                    model.State = Definition.STATE_CONFIRM;
                    model.ConfirmedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                    model.Confirmed = profile.UserCode;
                    if (!sManager.Save(model, out message)) { return false; }
                }
            }
            return result;
        }
        public override bool Recall(long modelid, out string message)
        {
            bool result = orgQuoteManager.Recall(modelid, out message);
            if (result)
            {
                IList<QuoteDetailModel> models = dFetcher.ListByMainId(profile.SiteId, modelid, out message, out rows);
                foreach (QuoteDetailModel model in models)
                {
                    model.ResetUpdateTrace();
                    model.State = Definition.STATE_NORMAL;
                    if (!odManager.Save(model, out message)) { return false; }
                }
                IList<RegionQuoteModel> rmodels = rFetcher.ListByMainId(profile.SiteId, modelid, out message, out rows);
                foreach (RegionQuoteModel model in rmodels)
                {
                    model.ResetUpdateTrace();
                    model.State = Definition.STATE_NORMAL;
                    if (!rManager.Save(model, out message)) { return false; }
                }
                IList<TitleQuoteModel> tmodels = tFetcher.ListByMainId(profile.SiteId, modelid, out message, out rows);
                foreach (TitleQuoteModel model in tmodels)
                {
                    model.ResetUpdateTrace();
                    model.State = Definition.STATE_NORMAL;
                    if (!tManager.Save(model, out message)) { return false; }
                }
                IList<SalesPointQuoteModel> smodels = sFetcher.ListByMainId(profile.SiteId, modelid, out message, out rows);
                foreach (SalesPointQuoteModel model in smodels)
                {
                    model.ResetUpdateTrace();
                    model.State = Definition.STATE_NORMAL;
                    if (!sManager.Save(model, out message)) { return false; }
                }
            }
            return result;
        }
        public override bool Reject(long id, out string message)
        {
            bool result = orgQuoteManager.Reject(id, out message);
            if (result)
            {
                IList<QuoteDetailModel> models = dFetcher.ListByMainId(profile.SiteId, id, out message, out rows);
                foreach (QuoteDetailModel model in models)
                {
                    model.ResetUpdateTrace();
                    model.State = Definition.STATE_NORMAL;
                    model.ApprovedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                    model.Approver = profile.UserCode;
                    if (!odManager.Save(model, out message)) { return false; }
                }
                IList<RegionQuoteModel> rmodels = rFetcher.ListByMainId(profile.SiteId, id, out message, out rows);
                foreach (RegionQuoteModel model in rmodels)
                {
                    model.ResetUpdateTrace();
                    model.State = Definition.STATE_NORMAL;
                    model.ApprovedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                    model.Approver = profile.UserCode;
                    if (!rManager.Save(model, out message)) { return false; }
                }
                IList<TitleQuoteModel> tmodels = tFetcher.ListByMainId(profile.SiteId, id, out message, out rows);
                foreach (TitleQuoteModel model in tmodels)
                {
                    model.ResetUpdateTrace();
                    model.State = Definition.STATE_NORMAL;
                    model.ApprovedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                    model.Approver = profile.UserCode;
                    if (!tManager.Save(model, out message)) { return false; }
                }
                IList<SalesPointQuoteModel> smodels = sFetcher.ListByMainId(profile.SiteId, id, out message, out rows);
                foreach (SalesPointQuoteModel model in smodels)
                {
                    model.ResetUpdateTrace();
                    model.State = Definition.STATE_NORMAL;
                    model.ApprovedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                    model.Approver = profile.UserCode;
                    if (!sManager.Save(model, out message)) { return false; }
                }
            }
            return result;
        }
        public override bool Revert(long modelid, out string message)
        {
            bool result = orgQuoteManager.Revert(modelid, out message);
            if (result)
            {
                IList<QuoteDetailModel> models = dFetcher.ListByMainId(profile.SiteId, modelid, out message, out rows);
                foreach (QuoteDetailModel model in models)
                {
                    model.ResetUpdateTrace();
                    model.State = Definition.STATE_NORMAL;
                    model.ApprovedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                    model.Approver = profile.UserCode;
                    if (!odManager.Save(model, out message)) { return false; }
                }
                IList<RegionQuoteModel> rmodels = rFetcher.ListByMainId(profile.SiteId, modelid, out message, out rows);
                foreach (RegionQuoteModel model in rmodels)
                {
                    model.ResetUpdateTrace();
                    model.State = Definition.STATE_NORMAL;
                    model.ApprovedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                    model.Approver = profile.UserCode;
                    if (!rManager.Save(model, out message)) { return false; }
                }
                IList<TitleQuoteModel> tmodels = tFetcher.ListByMainId(profile.SiteId, modelid, out message, out rows);
                foreach (TitleQuoteModel model in tmodels)
                {
                    model.ResetUpdateTrace();
                    model.State = Definition.STATE_NORMAL;
                    model.ApprovedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                    model.Approver = profile.UserCode;
                    if (!tManager.Save(model, out message)) { return false; }
                }
                IList<SalesPointQuoteModel> smodels = sFetcher.ListByMainId(profile.SiteId, modelid, out message, out rows);
                foreach (SalesPointQuoteModel model in smodels)
                {
                    model.ResetUpdateTrace();
                    model.State = Definition.STATE_NORMAL;
                    model.ApprovedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                    model.Approver = profile.UserCode;
                    if (!sManager.Save(model, out message)) { return false; }
                }
            }
            return result;
        }
        public override bool Submit(long modelid, out string message)
        {
            bool result = orgQuoteManager.Submit(modelid, out message);
            if (result)
            {
                IList<QuoteDetailModel> models = dFetcher.ListByMainId(profile.SiteId, modelid, out message, out rows);
                foreach (QuoteDetailModel model in models)
                {
                    model.ResetUpdateTrace();
                    model.State = Definition.STATE_READY;
                    if (!odManager.Save(model, out message)) { return false; }
                }
                IList<RegionQuoteModel> rmodels = rFetcher.ListByMainId(profile.SiteId, modelid, out message, out rows);
                foreach (RegionQuoteModel model in rmodels)
                {
                    model.ResetUpdateTrace();
                    model.State = Definition.STATE_READY;
                    if (!rManager.Save(model, out message)) { return false; }
                }
                IList<TitleQuoteModel> tmodels = tFetcher.ListByMainId(profile.SiteId, modelid, out message, out rows);
                foreach (TitleQuoteModel model in tmodels)
                {
                    model.ResetUpdateTrace();
                    model.State = Definition.STATE_READY;
                    if (!tManager.Save(model, out message)) { return false; }
                }
                IList<SalesPointQuoteModel> smodels = sFetcher.ListByMainId(profile.SiteId, modelid, out message, out rows);
                foreach (SalesPointQuoteModel model in smodels)
                {
                    model.ResetUpdateTrace();
                    model.State = Definition.STATE_READY;
                    if (!sManager.Save(model, out message)) { return false; }
                }
            }
            return result;
        }
    }
}
